import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

import vuetify from "@/plugins/vuetify"; // path to vuetify export
import VueLazyload from "vue-lazyload";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import VueStorage from "vue-ls";
import VueClipboard from "vue-clipboard2";
VueClipboard.config.autoSetContainer = true;
Vue.use(VueClipboard);
Vue.config.productionTip = false;

Vue.use(VueLazyload);
Vue.use(VueStorage);

// 路由守卫
// const whiteList = ["/user/login", "/login/forgetpass"]; // 白名单
router.beforeEach(async (to, from, next) => {
  NProgress.start();
  // console.log('to', to, 'form', from, 'next', next)
  const token = Vue.ls.get("token"); //本地获取token
  window.document.title = to.meta.title + "- 首单礼金";
  if (to.path == "/") {
    next({
      path: "/tlj"
    });
  } else if (to.path == "/user/login") {
    next();
  } else {
    next();
  }

  if (token) {
    //判断用户是否登录
    if (Object.keys(from.query).length === 0) {
      //判断路由来源是否有query，处理不是目的跳转的情况
      next();
    } else {
      const redirect = from.query.redirect; //如果来源路由有query
      if (to.path === redirect) {
        //这行是解决next无限循环的问题
        next();
      } else {
        next({ path: redirect }); //跳转到目的路由
      }
    }
  }
});
router.afterEach(() => {
  NProgress.done();
});

new Vue({
  router,
  store,
  vuetify,
  render: h => h(App)
}).$mount("#app");
